package com.nbsaas.boot.model.utils;

import com.alibaba.fastjson2.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.nbsaas.boot.rest.response.MapResponse;
import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class FieldTypeMatch {


    /**
     * 匹配类型,减少sql注入
     *
     * @param key
     * @param fieldType
     * @param filter
     * @return
     */
    public static Object matchType(String key, Integer fieldType, JSONObject filter) {
        /**
         * 字段类型字段类型 字段类型 1、字符串 2、Integer，3，Long，3 Float，4 Double，5，BigDecimal 6、Date 7、Boolean 8、Enum 9、对象 10、字符数组,11,整形数组，12小数数组
         */
        Object value = null;
        if (fieldType == 1) {
            value = filter.getString(key);
        } else if (fieldType == 2) {
            value = filter.getInteger(key);
        } else if (fieldType == 3) {
            value = filter.getLong(key);
        } else if (fieldType == 4) {
            value = filter.getDouble(key);
        } else if (fieldType == 5) {
            value = filter.getBigDecimal(key);
        } else if (fieldType == 6) {
            value = filter.getDate(key);
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            value = format.format(value);
        } else if (fieldType == 7) {
            value = filter.getBoolean(key);
        } else if (fieldType == 10) {
            //字符数组
            String temp = filter.getString(key);
            if (temp == null) {
                return null;
            }
            String[] strs = temp.split(",");
            value = Arrays.stream(strs).
                    map(String::trim).
                    filter(StringUtils::hasText).
                    map(s -> "'" + s + "'").
                    collect(Collectors.joining(","));
            value = "(" + value + ")";
        } else if (fieldType == 11) {
            //整形数组
            String temp = filter.getString(key);
            if (temp == null) {
                return null;
            }
            String[] stars = temp.split(",");
            value = Arrays.stream(stars).
                    map(String::trim).
                    filter(StringUtils::hasText).
                    map(Long::valueOf).
                    map(String::valueOf).
                    collect(Collectors.joining(","));
            value = "(" + value + ")";
        } else if (fieldType == 12) {
            //小数数组
            String temp = filter.getString(key);
            if (temp == null) {
                return null;
            }
            String[] stars = temp.split(",");
            value = Arrays.stream(stars).
                    map(String::trim).
                    filter(StringUtils::hasText).
                    map(Double::valueOf).
                    map(String::valueOf).
                    collect(Collectors.joining(","));
            value = "(" + value + ")";
        } else if (fieldType == 13) {
            //map对象
            value = filter.getJSONObject(key);
        } else if (fieldType == 101) {
            //字符串
            value = filter.getString(key);
        } else if (fieldType == 200) {
            //json 对象
            value = filter.getJSONObject(key).toJSONString();
        } else if (fieldType == 201) {
            //json 数组
            value = filter.getJSONArray(key).toJSONString();
        } else if (fieldType == 301) {
            //数组字符串
            List<String> temps = filter.getJSONArray(key).toList(String.class);
            value = temps.stream().map(s -> "'" + s + "'").collect(Collectors.joining(","));
            value = "(" + value + ")";
        } else if (fieldType == 302) {
            //数组数字
            List<Integer> temps = filter.getJSONArray(key).toList(Integer.class);
            value = temps.stream().map(String::valueOf).collect(Collectors.joining(","));
            value = "(" + value + ")";
        } else if (fieldType == 303) {
            //数组字长整型
            List<Long> temps = filter.getJSONArray(key).toList(Long.class);
            value = temps.stream().map(String::valueOf).collect(Collectors.joining(","));
            value = "(" + value + ")";
        } else if (fieldType == 401) {
            //全map对象
            ObjectMapper mapper = ObjectMapperUtils.getMapper();
            try {
                value = mapper.readValue(filter.getJSONObject(key).toJSONString(), MapResponse.class);
            }catch (Exception ignored){
            }
        }else if (fieldType == 402) {
            //全map对象List
            ObjectMapper mapper = ObjectMapperUtils.getMapper();
            try {
                value =mapper.readValue(filter.getJSONObject(key).toJSONString(), new TypeReference<List<MapResponse>>() {});
            }catch (Exception ignored){
            }
        }else {
            value = filter.getString(key);
        }
        //        ObjectMapper mapper = new ObjectMapper();
        return value;
    }
}
